﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Data;

public partial class ThongTin : System.Web.UI.Page
{
    public static bool Insert;
    protected void Page_Load(object sender, EventArgs e)
    {
        pnShow.Visible = true;
        pnUpdateCtSp.Visible = false;
        if (!IsPostBack)
        {
            BindGrid();
        }
    }

    public void BindGrid()
    {
        grdthongtinatm.DataSource = ThongtinService.db.ThongTinATM_GetByAll();
        grdthongtinatm.DataBind();
    }
    protected void btnThem_Click(object sender, EventArgs e)
    {
        pnUpdateCtSp.Visible = true;
        pnShow.Visible = false;
        //ControlClass.ResetControlValues(pnUpdate);
        drdlMakhuvuc.DataSource = ThongtinService.db.ThongTinATM_GetByMakhuvuc();
        drdlMakhuvuc.DataTextField = "Tinh";
        drdlMakhuvuc.DataValueField = "MakhuVuc";
        drdlMakhuvuc.DataBind();
        Insert = true;
    }
    protected void lbtUpdate_Click(object sender, EventArgs e)
    {
        var obj = new Thongtin();
        obj.MaATM = txtMaATM.Text;
        obj.MaKhuvuc = drdlMakhuvuc.SelectedValue;
        obj.TenmayATM = txttenmay.Text;
        obj.Diachi = txtdiachi.Text;
        obj.Latitude = txtlat.Text;

        obj.Longtitude = txtlong.Text;
        obj.ThongTin = txtthongtin.Text;
        obj.IsActive = txtkichhoat.Text;

        if (Insert)
        {
            ThongtinService.db.ThongTinATM_Insert(obj);
        }
        else
        {
            ThongtinService.db.ThongTinATM_Update(obj);
        }
        BindGrid();
        pnUpdateCtSp.Visible = false;
        pnShow.Visible = true;
        Insert = false;
    }
    protected void grdthongtinatm_PageIndexChanged(object source, DataGridPageChangedEventArgs e)
    {
        grdthongtinatm.CurrentPageIndex = e.NewPageIndex;
        BindGrid();
    }
    protected void grdthongtinatm_ItemCommand(object source, DataGridCommandEventArgs e)
    {
        Thongtin obj = new Thongtin();
        string MaATM = e.CommandArgument.ToString();
        if (e.CommandName == "Edit")
        {
            Insert = false;
            DataTable dt = ThongtinService.db.ThongTinATM_GetById(MaATM);// viet cho cau nay nay chay da, T tu viet di nh
            //drdlMakhuvuc.DataSource = ThongtinService.db.ThongTinATM_GetByMakhuvuc();// khong lay du lieu len duoc
            //drdlMakhuvuc.DataTextField = "MaKhuVuc";
            //drdlMakhuvuc.DataValueField = "MaKhuVuc";
            //drdlMakhuvuc.DataBind();
           // txttenmay.Text = dt.Rows[0]["TenmayATM"].ToString();
           // txtdiachi.Text = dt.Rows[0]["Diachi"].ToString();
          //  txtlat.Text = dt.Rows[0]["Latitude"].ToString();
           // txtlong.Text = dt.Rows[0]["Long"].ToString();
           // txtMaATM.Text = dt.Rows[0]["MaATM"].ToString();
            //txtthongtin.Text = dt.Rows[0]["ThongTin"].ToString();
            //txtkichhoat.Text = dt.Rows[0]["IsActive"].ToString();

            pnUpdateCtSp.Visible = true;
            pnShow.Visible = false;
        }
        else
        {
            ThongtinService.db.ThongTinATM_Delete(MaATM);
            BindGrid();
        }
    }
}